package com.cjw.controller.person;

import com.cjw.config.annotation.OperationLogAnnotation;
import com.cjw.enums.ErrorCode;
import com.cjw.enums.systemManage.ExamineType;
import com.cjw.exceptions.BusinessException;
import com.cjw.exceptions.ContextException;
import com.cjw.pojo.dto.database.PageDTO;
import com.cjw.pojo.dto.livesquare.MyLiveShareDTO;
import com.cjw.pojo.query.person.AddReadDTO;
import com.cjw.pojo.dto.soulread.MyReadDTO;
import com.cjw.pojo.entity.contentReview.TextReponse;
import com.cjw.service.contentReview.TextCensor;
import com.cjw.service.person.MyDesignService;
import com.cjw.service.systemManage.ExamineService;
import com.cjw.service.userMange.UserAccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @create_time: 2021.12.25
 * @author : cjw
 */
@RestController
@RequestMapping("/user/myDesign")
@Api("我的作品资源")
public class MyDesignController {
    @Autowired
    private MyDesignService myDesignService;

    @Autowired
    private ExamineService examineService;

    @Autowired
    private UserAccountService accountService;

    @PostMapping("/findAllLiveSharing")
    @ApiOperation("查看全部生活分享")
    @OperationLogAnnotation(operModul = "个人中心模块-我的作品",operType = "查看全部生活分享",operDesc = "查询")
    public List<MyLiveShareDTO> findAllLiveSharing(Integer page, Integer size) {
        return myDesignService.findAllLiveSharing(page,size);
    }

    @PostMapping("/open/close/powerLive")
    @ApiOperation("打开/关闭权限生活分享")
    @OperationLogAnnotation(operModul = "个人中心模块-我的作品",operType = "查看全部生活分享",operDesc = "更新")
    public Boolean OpenClosePower(Long liveId) {
        return myDesignService.OpenClosePowerLive(liveId);
    }

    @PostMapping("/delete/live")
    @ApiOperation("删除生活分享")
    @OperationLogAnnotation(operModul = "个人中心模块-我的作品",operType = "删除生活分享",operDesc = "删除")
    public Boolean deleteLive(Long liveId) {
        return myDesignService.deleteLive(liveId);
    }

    @PostMapping("/query/myRead")
    @ApiOperation("查询个人的全部朗读")
    @OperationLogAnnotation(operModul = "个人中心模块-我的作品",operType = "查询全部朗读",operDesc = "查询")
    public PageDTO<List<MyReadDTO>> queryMyRead(Integer page, Integer size, String find) {
        return myDesignService.findAllMyReadDTO(page, size, find);
    }

    @PostMapping("/delete/myRead")
    @ApiOperation("删除个人朗读")
    @OperationLogAnnotation(operModul = "个人中心模块-我的作品",operType = "删除个人朗读",operDesc = "删除")
    public Boolean deleteRead(Long readId) {
        return myDesignService.deleteRead(readId);
    }

    @PostMapping("/changePower/myRead")
    @ApiOperation("修改个人朗读权限")
    @OperationLogAnnotation(operModul = "个人中心模块-我的作品",operType = "修改个人朗读权限",operDesc = "修改")
    public Boolean changePowerMyRead(Long readId) {
        return myDesignService.changePowerRead(readId);
    }

    @PostMapping("/add/myRead")
    @ApiOperation("添加个人朗读")
    @OperationLogAnnotation(operModul = "个人中心模块-我的作品",operType = "添加个人朗读",operDesc = "添加")
    public Boolean addRead(@RequestBody AddReadDTO addReadDTO) {
        check(addReadDTO);
        Long readId = myDesignService.addRead(addReadDTO);
        examineService.addExamineByPerson(ExamineType.READ.getCode(), readId, addReadDTO.getContent());
        return readId != null;
    }

    @PostMapping("/add/myReadImage")
    @ApiOperation("添加朗读图片")
    @OperationLogAnnotation(operModul = "个人中心模块-我的作品",operType = "添加朗读图片",operDesc = "添加")
    public String upReadImage(MultipartFile file) {
        return myDesignService.upReadImage(file);
    }

    @PostMapping("/add/myReadVideo")
    @ApiOperation("添加朗读音频")
    @OperationLogAnnotation(operModul = "个人中心模块-我的作品",operType = "添加朗读音频",operDesc = "添加")
    public String upReadVideo(MultipartFile file) {
        return myDesignService.upReadVideo(file);
    }

    private Boolean check(AddReadDTO addReadDTO) {
        if (accountService.isInBlackList(ExamineType.READ.getCode())) {
            throw new BusinessException(ErrorCode.BANED);
        }
        TextReponse textReponse = TextCensor.TextCensor(addReadDTO.getContent());
        if (textReponse.getConclusion().equals("合规"))
            return true;
        else {
            examineService.addExamineByAuto(ExamineType.READ.getCode(), null, addReadDTO.getContent());
            throw new ContextException(textReponse.getData().get(0).getMsg());
        }
    }
}

